<template>
  <div class="tags-container">
    <div class="page-header">
      <h1>标签管理</h1>
      <button class="add-button" @click="showAddDialog = true">
        <i class="fa fa-plus"></i>
        添加标签
      </button>
    </div>
    
    <!-- 搜索和筛选 -->
    <div class="search-filter">
      <div class="search-box">
        <i class="fa fa-search"></i>
        <input 
          v-model="searchQuery" 
          type="text" 
          placeholder="搜索标签名称或描述"
          @input="handleSearch"
        />
      </div>
      <div class="filter-group">
        <select v-model="filterStatus" @change="handleFilter">
          <option value="">全部状态</option>
          <option value="active">启用</option>
          <option value="inactive">禁用</option>
        </select>
      </div>
    </div>
    
    <!-- 标签列表 -->
    <div class="tags-grid">
      <div v-for="tag in paginatedTags" :key="tag.id" class="tag-card">
        <div class="tag-header">
          <span class="tag-name">{{ tag.name }}</span>
          <span class="tag-count">{{ tag.videoCount }} 个视频</span>
        </div>
        <p v-if="tag.description" class="tag-description">{{ tag.description }}</p>
        <div class="tag-meta">
          <span class="tag-usage">{{ tag.usageCount }} 次使用</span>
          <span class="tag-status" :class="tag.status">
            {{ tag.status === 'active' ? '启用' : '禁用' }}
          </span>
        </div>
        <div class="tag-actions">
          <button class="action-btn edit-btn" @click="editTag(tag)">
            <i class="fa fa-edit"></i>
            编辑
          </button>
          <button class="action-btn status-btn" @click="toggleTagStatus(tag)">
            {{ tag.status === 'active' ? '禁用' : '启用' }}
          </button>
          <button class="action-btn delete-btn" :disabled="tag.videoCount > 0" @click="deleteTag(tag)">
            <i class="fa fa-trash"></i>
            删除
          </button>
        </div>
      </div>
      
      <!-- 空状态 -->
      <div v-if="filteredTags.length === 0" class="empty-state">
        <i class="fa fa-tags"></i>
        <p>暂无标签数据</p>
      </div>
    </div>
    
    <!-- 批量操作 -->
    <div v-if="filteredTags.length > 0" class="batch-operations">
      <div class="operations-left">
        <span>总计 {{ filteredTags.length }} 个标签</span>
      </div>
      <div class="operations-right">
        <button class="batch-btn" @click="selectAllTags">
          {{ allSelected ? '取消全选' : '全选' }}
        </button>
        <button class="batch-btn delete-btn" :disabled="selectedTags.length === 0" @click="batchDelete">
          批量删除
        </button>
      </div>
    </div>
    
    <!-- 分页 -->
    <div v-if="filteredTags.length > 0" class="pagination">
      <button :disabled="currentPage === 1" @click="currentPage = 1">
        <i class="fa fa-angle-double-left"></i>
      </button>
      <button :disabled="currentPage === 1" @click="currentPage--">
        <i class="fa fa-angle-left"></i>
      </button>
      <span class="page-info">
        第 {{ currentPage }} 页，共 {{ totalPages }} 页
      </span>
      <button :disabled="currentPage === totalPages" @click="currentPage++">
        <i class="fa fa-angle-right"></i>
      </button>
      <button :disabled="currentPage === totalPages" @click="currentPage = totalPages">
        <i class="fa fa-angle-double-right"></i>
      </button>
    </div>
    
    <!-- 添加/编辑标签对话框 -->
    <div v-if="showAddDialog || showEditDialog" class="dialog-overlay" @click="closeDialog">
      <div class="dialog" @click.stop>
        <div class="dialog-header">
          <h3>{{ showEditDialog ? '编辑标签' : '添加标签' }}</h3>
          <button class="close-button" @click="closeDialog">
            <i class="fa fa-times"></i>
          </button>
        </div>
        <div class="dialog-content">
          <form @submit.prevent="submitTagForm">
            <div class="form-group">
              <label>标签名称</label>
              <input v-model="tagForm.name" type="text" required />
              <span class="form-tip">标签名称将显示在视频下方</span>
            </div>
            <div class="form-group">
              <label>标签描述</label>
              <textarea v-model="tagForm.description" rows="3" placeholder="可选，添加标签的详细说明"></textarea>
            </div>
            <div class="form-group">
              <label>标签颜色</label>
              <div class="color-selector">
                <button 
                  v-for="color in availableColors" 
                  :key="color" 
                  type="button"
                  :class="['color-option', { active: tagForm.color === color }]"
                  :style="{ backgroundColor: color }"
                  @click="tagForm.color = color"
                ></button>
              </div>
            </div>
            <div class="form-group">
              <label>标签状态</label>
              <select v-model="tagForm.status" required>
                <option value="active">启用</option>
                <option value="inactive">禁用</option>
              </select>
            </div>
            <div class="form-group">
              <label>是否热门标签</label>
              <input v-model="tagForm.isHot" type="checkbox" />
              <span class="form-tip">热门标签将显示在首页</span>
            </div>
            <div class="dialog-footer">
              <button type="button" class="cancel-button" @click="closeDialog">取消</button>
              <button type="submit" class="save-button">{{ showEditDialog ? '更新' : '添加' }}</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TagsView',
  data() {
    return {
      // 可用颜色列表
      availableColors: ['#3498db', '#2ecc71', '#e74c3c', '#f39c12', '#9b59b6', '#1abc9c', '#34495e', '#e67e22'],
      // 模拟标签数据
      tags: [
        {
          id: 'tag1',
          name: '新番推荐',
          description: '最新动漫番剧推荐和解析',
          color: '#3498db',
          status: 'active',
          isHot: true,
          videoCount: 128,
          usageCount: 567,
        },
        {
          id: 'tag2',
          name: '音乐现场',
          description: '演唱会、音乐节、音乐现场表演',
          color: '#2ecc71',
          status: 'active',
          isHot: true,
          videoCount: 98,
          usageCount: 432,
        },
        {
          id: 'tag3',
          name: '游戏实况',
          description: '游戏直播录像、游戏攻略',
          color: '#e74c3c',
          status: 'active',
          isHot: true,
          videoCount: 234,
          usageCount: 890,
        },
        {
          id: 'tag4',
          name: '美食教程',
          description: '烹饪技巧、美食制作教程',
          color: '#f39c12',
          status: 'active',
          isHot: false,
          videoCount: 76,
          usageCount: 234,
        },
        {
          id: 'tag5',
          name: '生活技巧',
          description: '日常生活小窍门、生活妙招',
          color: '#9b59b6',
          status: 'active',
          isHot: false,
          videoCount: 45,
          usageCount: 156,
        },
        {
          id: 'tag6',
          name: '科技评测',
          description: '数码产品、科技产品评测',
          color: '#1abc9c',
          status: 'active',
          isHot: true,
          videoCount: 87,
          usageCount: 345,
        },
        {
          id: 'tag7',
          name: '旅行攻略',
          description: '旅行目的地介绍、旅行经验分享',
          color: '#34495e',
          status: 'active',
          isHot: false,
          videoCount: 65,
          usageCount: 210,
        },
        {
          id: 'tag8',
          name: '学习分享',
          description: '学习方法、知识分享',
          color: '#e67e22',
          status: 'active',
          isHot: false,
          videoCount: 34,
          usageCount: 132,
        },
        {
          id: 'tag9',
          name: '搞笑视频',
          description: '幽默、搞笑、娱乐视频',
          color: '#3498db',
          status: 'active',
          isHot: true,
          videoCount: 156,
          usageCount: 678,
        },
        {
          id: 'tag10',
          name: 'vlog',
          description: '日常记录、个人日记视频',
          color: '#2ecc71',
          status: 'active',
          isHot: false,
          videoCount: 102,
          usageCount: 456,
        },
        {
          id: 'tag11',
          name: '影视解说',
          description: '电影、电视剧解析和评论',
          color: '#e74c3c',
          status: 'active',
          isHot: true,
          videoCount: 89,
          usageCount: 342,
        },
        {
          id: 'tag12',
          name: '时尚潮流',
          description: '时尚穿搭、美妆、潮流资讯',
          color: '#f39c12',
          status: 'active',
          isHot: false,
          videoCount: 56,
          usageCount: 213,
        },
        {
          id: 'tag13',
          name: '健康养生',
          description: '健康知识、养生方法、运动健身',
          color: '#9b59b6',
          status: 'active',
          isHot: false,
          videoCount: 43,
          usageCount: 167,
        },
        {
          id: 'tag14',
          name: '财经资讯',
          description: '财经新闻、投资分析',
          color: '#1abc9c',
          status: 'active',
          isHot: false,
          videoCount: 32,
          usageCount: 145,
        },
        {
          id: 'tag15',
          name: '汽车测评',
          description: '汽车评测、汽车知识',
          color: '#34495e',
          status: 'inactive',
          isHot: false,
          videoCount: 0,
          usageCount: 0,
        },
      ],
      searchQuery: '',
      filterStatus: '',
      currentPage: 1,
      itemsPerPage: 12,
      showAddDialog: false,
      showEditDialog: false,
      allSelected: false,
      selectedTags: [],
      tagForm: {
        id: '',
        name: '',
        description: '',
        color: '#3498db',
        status: 'active',
        isHot: false,
        videoCount: 0,
        usageCount: 0,
      },
    }
  },
  computed: {
    filteredTags() {
      let filtered = this.tags
      
      // 搜索
      if (this.searchQuery) {
        const query = this.searchQuery.toLowerCase()
        filtered = filtered.filter(tag => 
          tag.name.toLowerCase().includes(query) || 
          (tag.description && tag.description.toLowerCase().includes(query)),
        )
      }
      
      // 状态筛选
      if (this.filterStatus) {
        filtered = filtered.filter(tag => tag.status === this.filterStatus)
      }
      
      return filtered
    },
    paginatedTags() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.filteredTags.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.filteredTags.length / this.itemsPerPage)
    },
  },
  methods: {
    handleSearch() {
      this.currentPage = 1
      this.allSelected = false
      this.selectedTags = []
    },
    handleFilter() {
      this.currentPage = 1
      this.allSelected = false
      this.selectedTags = []
    },
    editTag(tag) {
      this.tagForm = { ...tag }
      this.showEditDialog = true
    },
    toggleTagStatus(tag) {
      const newStatus = tag.status === 'active' ? 'inactive' : 'active'
      if (confirm(`确定要${newStatus === 'active' ? '启用' : '禁用'}标签 "${tag.name}" 吗？`)) {
        // 在实际应用中，这里应该调用API
        tag.status = newStatus
      }
    },
    deleteTag(tag) {
      if (tag.videoCount > 0) {
        alert('该标签下还有视频，无法删除！')
        return
      }
      
      if (confirm(`确定要删除标签 "${tag.name}" 吗？此操作不可撤销！`)) {
        // 在实际应用中，这里应该调用API
        const index = this.tags.findIndex(t => t.id === tag.id)
        if (index > -1) {
          this.tags.splice(index, 1)
          // 从选中列表中移除
          this.selectedTags = this.selectedTags.filter(id => id !== tag.id)
        }
      }
    },
    selectAllTags() {
      this.allSelected = !this.allSelected
      if (this.allSelected) {
        this.selectedTags = this.paginatedTags.map(tag => tag.id)
      } else {
        this.selectedTags = []
      }
    },
    batchDelete() {
      const deletableTags = this.paginatedTags.filter(tag => 
        this.selectedTags.includes(tag.id) && tag.videoCount === 0,
      )
      
      const nonDeletableTags = this.paginatedTags.filter(tag => 
        this.selectedTags.includes(tag.id) && tag.videoCount > 0,
      )
      
      if (nonDeletableTags.length > 0) {
        alert(`部分标签下还有视频，无法删除：\n${nonDeletableTags.map(tag => tag.name).join('、')}`)
        if (deletableTags.length === 0) return
      }
      
      if (confirm(`确定要删除选中的 ${deletableTags.length} 个标签吗？此操作不可撤销！`)) {
        // 在实际应用中，这里应该调用API
        deletableTags.forEach(tag => {
          const index = this.tags.findIndex(t => t.id === tag.id)
          if (index > -1) {
            this.tags.splice(index, 1)
          }
        })
        this.selectedTags = []
        this.allSelected = false
      }
    },
    closeDialog() {
      this.showAddDialog = false
      this.showEditDialog = false
      this.resetForm()
    },
    resetForm() {
      this.tagForm = {
        id: '',
        name: '',
        description: '',
        color: '#3498db',
        status: 'active',
        isHot: false,
        videoCount: 0,
        usageCount: 0,
      }
    },
    generateId(name) {
      return 'tag' + (this.tags.length + 1)
    },
    submitTagForm() {
      if (this.showEditDialog) {
        // 编辑标签
        const index = this.tags.findIndex(t => t.id === this.tagForm.id)
        if (index > -1) {
          this.tags[index] = { ...this.tags[index], ...this.tagForm }
        }
      } else {
        // 添加标签
        const newTag = {
          ...this.tagForm,
          id: this.generateId(this.tagForm.name),
          videoCount: 0,
          usageCount: 0,
        }
        this.tags.push(newTag)
      }
      
      this.closeDialog()
    },
  },
}
</script>

<style scoped>
.tags-container {
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h1 {
  color: #2c3e50;
  margin: 0;
  font-size: 24px;
}

.add-button {
  background: #3498db;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  transition: background-color 0.3s;
}

.add-button:hover {
  background: #2980b9;
}

.add-button i {
  margin-right: 5px;
}

/* 搜索和筛选 */
.search-filter {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  gap: 20px;
  flex-wrap: wrap;
}

.search-box {
  position: relative;
  flex: 1;
  min-width: 300px;
}

.search-box i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #95a5a6;
}

.search-box input {
  width: 100%;
  padding: 10px 15px 10px 40px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.filter-group select {
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  background: white;
}

/* 标签网格 */
.tags-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 15px;
  margin-bottom: 20px;
}

.tag-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  transition: transform 0.3s, box-shadow 0.3s;
  position: relative;
  overflow: hidden;
  border-left: 4px solid #3498db;
}

.tag-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(0,0,0,0.15);
}

.tag-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
}

.tag-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.tag-name {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
}

.tag-count {
  font-size: 12px;
  color: #7f8c8d;
  background: #ecf0f1;
  padding: 2px 8px;
  border-radius: 12px;
}

.tag-description {
  font-size: 14px;
  color: #7f8c8d;
  margin: 0 0 10px 0;
  line-height: 1.5;
}

.tag-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.tag-usage {
  font-size: 12px;
  color: #3498db;
}

.tag-status {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.tag-status.active {
  background: #2ecc71;
  color: white;
}

.tag-status.inactive {
  background: #95a5a6;
  color: white;
}

.tag-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: background-color 0.3s;
  display: flex;
  align-items: center;
  gap: 4px;
}

.edit-btn {
  background: #3498db;
  color: white;
}

.edit-btn:hover {
  background: #2980b9;
}

.status-btn {
  background: #f39c12;
  color: white;
}

.status-btn:hover {
  background: #e67e22;
}

.delete-btn {
  background: #e74c3c;
  color: white;
}

.delete-btn:hover:not(:disabled) {
  background: #c0392b;
}

.delete-btn:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
  opacity: 0.6;
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
  color: #95a5a6;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 15px;
  opacity: 0.5;
}

/* 批量操作 */
.batch-operations {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 6px;
  margin-bottom: 20px;
}

.operations-left span {
  font-size: 14px;
  color: #7f8c8d;
}

.operations-right {
  display: flex;
  gap: 10px;
}

.batch-btn {
  padding: 8px 16px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.batch-btn:hover:not(:disabled) {
  background: #f8f9fa;
  border-color: #3498db;
  color: #3498db;
}

.batch-btn.delete-btn {
  border-color: #e74c3c;
  color: #e74c3c;
}

.batch-btn.delete-btn:hover:not(:disabled) {
  background: #e74c3c;
  color: white;
}

.batch-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* 分页 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
}

.pagination button {
  padding: 8px 12px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.pagination button:hover:not(:disabled) {
  background: #f8f9fa;
  border-color: #3498db;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  font-size: 14px;
  color: #666;
}

/* 对话框 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.dialog-header h3 {
  margin: 0;
  color: #2c3e50;
}

.close-button {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #95a5a6;
}

.dialog-content {
  padding: 20px;
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: #34495e;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.form-tip {
  font-size: 12px;
  color: #95a5a6;
  margin-top: 5px;
  display: block;
}

.form-group input[type="checkbox"] {
  width: auto;
  margin-right: 10px;
}

/* 颜色选择器 */
.color-selector {
  display: flex;
  gap: 10px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.color-option {
  width: 40px;
  height: 40px;
  border: 2px solid transparent;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.color-option:hover {
  transform: scale(1.1);
}

.color-option.active {
  border-color: #34495e;
  transform: scale(1.1);
  box-shadow: 0 3px 8px rgba(0,0,0,0.2);
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 30px;
}

.cancel-button,
.save-button {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background-color 0.3s;
}

.cancel-button {
  background: #ecf0f1;
  color: #7f8c8d;
}

.cancel-button:hover {
  background: #bdc3c7;
}

.save-button {
  background: #3498db;
  color: white;
}

.save-button:hover {
  background: #2980b9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-filter {
    flex-direction: column;
  }
  
  .operations-left,
  .operations-right {
    flex: 1;
  }
  
  .operations-right {
    justify-content: flex-end;
  }
  
  .tags-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  }
  
  .pagination {
    flex-wrap: wrap;
  }
}

@media (max-width: 480px) {
  .batch-operations {
    flex-direction: column;
    gap: 10px;
    align-items: flex-start;
  }
  
  .operations-right {
    width: 100%;
    justify-content: flex-start;
  }
  
  .tag-actions {
    flex-wrap: wrap;
  }
  
  .action-btn {
    flex: 1;
    justify-content: center;
  }
}
</style>